package object3D;

import context.*;

import geometry3D.*;
import util.Projection;
import util.Transform;
import util.Util;

import java.awt.*;
import java.awt.geom.*;
import java.util.*;


// Java 3D
import javax.media.j3d.*;

import com.sun.j3d.utils.geometry.Box;
import javax.vecmath.*;


// ********************************************************************
// *** CLASS: Block
// ********************************************************************
public class CenterBlock extends Block implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
    
  public Vector<CenterBlock> extras; // Extra object attached to this object
  
  protected double rotateX=0; // Clockwise rotation in radians
  protected double rotateY=0; // Clockwise rotation in radians
  protected double rotateZ=0; // Clockwise rotation in radians
  public void setRotate(double rx,double ry,double rz)
  { rotateX=rx; rotateY=ry; rotateZ=rz; }
  
      
  public CenterBlock(String ident,Point3D p0,double width,double height,double depth)
  { super(ident,p0,width,height,depth);
    this.extras=new Vector<CenterBlock>();
  }
  
  public boolean contains(Point3D p)
  { if( (p.x>=p0.x) & (p.x<=(p0.x+width)) )
    { if( (p.y>=p0.y) & (p.y<=(p0.x+height)) )
      { if( (p.z>=p0.z) & (p.z<=(p0.z+depth)) ) return(true); }
    }
    return(false);
  }
  
  // ***************************************************************************
  // *** METHOD: getModel3D
  // ***************************************************************************
  public /*abstract*/ Group getModel3D(Appearance appearance)
  { TransformGroup tg=new TransformGroup();
    Transform3D transform=new Transform3D();
    double x=p0.x;//+width/2;
    double y=p0.y;//+height/2;
    double z=p0.z;//+depth/2;
    transform.setTranslation(new Vector3f((float)x,(float)y,(float)z));
    transform=Transform.rotate(transform,rotateX,rotateY,rotateZ);
    tg.setTransform(transform);
//    Box box=new Box((float)width/2,(float)height/2,(float)depth/2,appearance);
    Box box=new Box((float)width,(float)height,(float)depth,appearance);
    tg.addChild(box);
    return(tg);
  }  
  
  // 3D ==> 2D projections the XZ, XY or ZY-plane
  // 'origo2D' position of origo in 2D coordinates
  public Rectangle2D.Double projection(int direction,Point2D.Double origo2D,double scale)
  { return(Projection.blockProjection(this,direction,origo2D,scale)); }
    
  public void drawProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  { Rectangle2D.Double r=projection(direction,origo2D,scale);
    Util.fill(g,r,c);
    Util.draw(g,r);
    if(direction==HCUT) drawExtraProjection(HCUT,g,origo2D,scale,c);
  }
   
  public void addExtra(CenterBlock extra)
  { this.extras.add(extra); }  
    
  public void drawExtraProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  { for(Enumeration e=extras.elements();e.hasMoreElements();)
    { CenterBlock extra=(CenterBlock)e.nextElement();
      extra.drawProjection(direction,g,origo2D,scale,Util.rammeFarge);
    }
  }
    
  public CenterBlock getExtra(String ident)
  { for(Enumeration e=extras.elements();e.hasMoreElements();)
    { CenterBlock extra=(CenterBlock)e.nextElement();
      if(extra.ident.equals(ident)) return(extra);
    }
    return(null);
  }
}  